package com.itheima.service.mongo.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.itheima.domain.mongo.UserLocation;
import com.itheima.service.mongo.UserLocationService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.geo.Circle;
import org.springframework.data.geo.Distance;
import org.springframework.data.geo.Metrics;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.geo.GeoJsonPoint;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserLocationServiceImpl implements UserLocationService {

    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 保存或者修改位置信息
     *
     * @param latitude  维度
     * @param longitude 经度
     * @param addrStr   位置描述
     * @param userId    登录用户id
     */
    @Override
    public void saveOrUpdateUserLocation(Double latitude, Double longitude, String addrStr, Long userId) {

        System.out.println("纬度" + latitude);
        System.out.println("经度" + longitude);
        Query query = new Query(Criteria.where("userId").is(userId));
        UserLocation userLocation = mongoTemplate.findOne(query, UserLocation.class);
        if (userLocation == null) {
            userLocation = new UserLocation();
            userLocation.setUserId(userId);
            userLocation.setAddress(addrStr);
            userLocation.setCreated(System.currentTimeMillis());
            userLocation.setUpdated(System.currentTimeMillis());
            userLocation.setLastUpdated(System.currentTimeMillis());
            GeoJsonPoint point = new GeoJsonPoint(longitude, latitude);
            userLocation.setLocation(point);
            mongoTemplate.save(userLocation);
        } else {
            userLocation.setAddress(addrStr);
            userLocation.setLastUpdated(userLocation.getUpdated());//将上一次的时间保存一下
            userLocation.setUpdated(System.currentTimeMillis()); //更新当前时间
            GeoJsonPoint point = new GeoJsonPoint(longitude, latitude);
            userLocation.setLocation(point);
            mongoTemplate.save(userLocation);
        }
    }

    /**
     * 查找附近的人
     *
     * @param gender   性别
     * @param distance 距离
     * @param userId   用户id
     * @return 附近的人id
     */
    @Override
    public List<Long> findNearbyUser(String gender, Integer distance, Long userId) {
        //获取我的位置
        Query query = new Query(Criteria.where("userId").is(userId));
        UserLocation userLocation = mongoTemplate.findOne(query, UserLocation.class);
        //确定圆的半径
        Distance cirDistance = new Distance(distance/1000 , Metrics.KILOMETERS);
        //画圆,之前半径传错了
        Circle circle = new Circle(userLocation.getLocation(), cirDistance);
        //查询圆形范围内的用户
        Query query1 = new Query(Criteria.where("location").withinSphere(circle));
        List<UserLocation> locationList = mongoTemplate.find(query1, UserLocation.class);
        //构建返回对象集合
        List<Long> ids = new ArrayList<>();
        //获取圆中用户的id
        if (CollectionUtil.isNotEmpty(locationList)) {
            for (UserLocation location : locationList) {
                ids.add(location.getUserId());
            }
        }
        return ids;
    }

    @Override
    public Map findAddress(Long userId) {
        UserLocation userLocation = mongoTemplate.findOne(new Query(Criteria.where("userId").is(userId)), UserLocation.class);
        //获取坐标
        GeoJsonPoint location = userLocation.getLocation();
        Map<String, Object> map = new HashMap<>();
        map.put("Longitude", location.getX());
        map.put("Latitude", location.getY());
        map.put("address", userLocation.getAddress());
        return map;
    }
}
